package yaner.hotel.manager.resource.service.impl;

import org.redisson.api.RAtomicLong;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;
import yaner.hotel.manager.common.dto.order.OrderAddDto;
import yaner.hotel.manager.common.dto.order.OrderDto;
import yaner.hotel.manager.common.dto.order.OrderUpdateDto;
import yaner.hotel.manager.common.utils.DateUtils;
import yaner.hotel.manager.common.vo.OrderVo;
import yaner.hotel.manager.resource.dao.IOrderDao;
import yaner.hotel.manager.resource.entity.Order;
import yaner.hotel.manager.resource.mapper.IOrderMapper;
import yaner.hotel.manager.resource.service.IOrderService;

import java.util.List;

@Service
public class OrderServiceImpl implements IOrderService {
    private final IOrderDao orderDao;
    private final IOrderMapper orderMapper;
    private final RedissonClient redissonClient;

    public OrderServiceImpl(IOrderDao orderDao, IOrderMapper orderMapper, RedissonClient redissonClient) {
        this.orderDao = orderDao;
        this.orderMapper = orderMapper;
        this.redissonClient = redissonClient;
    }

    @Override
    public List<OrderVo> orderList(OrderDto orderDto) {
        List<OrderVo> vos = orderDao.listAll(orderDto);
        vos.forEach(orderVo -> {
            orderVo.setOrderStatusStr(orderVo.getOrderStatus().getDesc());
            orderVo.setPaymentTypeStr(orderVo.getPaymentType().getDesc());
            orderVo.setOrderTypeStr(orderVo.getOrderType().getDesc());
        });
        return vos;
    }

    @Override
    public OrderVo getOrderInfo(String orderId) {
        Order order = orderDao.selectById(orderId);
        return orderMapper.toVo(order);
    }

    @Override
    public String generateOrderCode() {
        RAtomicLong order_code = redissonClient.getAtomicLong("order_code");
        return "DD"+ DateUtils.getCurrentDate() + order_code.incrementAndGet();
    }

    @Override
    public String addOrderInfo(OrderAddDto orderAddDto) {
        int i = orderDao.insert(orderMapper.toAddEntity(orderAddDto));
        return String.valueOf(i);
    }

    @Override
    public String updateOrderInfo(OrderUpdateDto orderUpdateDto) {
        Order order = orderMapper.toUpdateEntity(orderUpdateDto);
        order.setOrderId(orderUpdateDto.getOrderId());
        int i = orderDao.updateById(order);
        return String.valueOf(i);
    }

    @Override
    public String deleteOrderInfo(String orderId) {
        int i = orderDao.deleteById(orderId);
        return String.valueOf(i);
    }
}
